package com.lifeverse.factory.type;

import com.lifeverse.entity.LifeEntity;
import com.lifeverse.entity.enums.LifeEntityType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 概念生命体创建器
 * 专门创建概念类型的生命体
 */
@Slf4j
@Component
public class ConceptualLifeEntityCreator implements LifeEntityCreator {
    
    @Override
    public LifeEntity create(String name, String description, Map<String, Object> configuration) {
        log.info("Creating conceptual life entity: {}", name);
        
        LifeEntity entity = new LifeEntity();
        entity.setType(LifeEntityType.CONCEPTUAL);
        
        // 设置概念生命体特有的初始属性
        setupConceptualAttributes(entity, configuration);
        
        return entity;
    }
    
    @Override
    public LifeEntity clone(LifeEntity originalEntity, String newName, double variationLevel) {
        log.info("Cloning conceptual life entity: {} to {}", originalEntity.getName(), newName);
        
        LifeEntity clonedEntity = new LifeEntity();
        clonedEntity.setType(LifeEntityType.CONCEPTUAL);
        
        // 复制原始概念的属性
        copyConceptualAttributes(clonedEntity, originalEntity);
        
        return clonedEntity;
    }
    
    @Override
    public boolean validateConfiguration(Map<String, Object> configuration) {
        if (configuration == null) {
            return true; // 允许空配置，使用默认值
        }
        
        // 验证概念特有配置
        if (configuration.containsKey("abstractionLevel")) {
            Object level = configuration.get("abstractionLevel");
            if (!(level instanceof Number)) {
                return false;
            }
            int value = ((Number) level).intValue();
            if (value < 1 || value > 10) {
                return false;
            }
        }
        
        if (configuration.containsKey("complexityScore")) {
            Object complexity = configuration.get("complexityScore");
            if (!(complexity instanceof Number)) {
                return false;
            }
            double value = ((Number) complexity).doubleValue();
            if (value < 0 || value > 100) {
                return false;
            }
        }
        
        if (configuration.containsKey("influenceRadius")) {
            Object radius = configuration.get("influenceRadius");
            if (!(radius instanceof Number) || ((Number) radius).doubleValue() < 0) {
                return false;
            }
        }
        
        return true;
    }
    
    @Override
    public Map<String, Object> getDefaultConfiguration() {
        return Map.of(
                "conceptCategory", "Abstract",
                "abstractionLevel", 5, // 1-10级抽象程度
                "complexityScore", 60.0,
                "influenceRadius", 1000.0, // 影响半径
                "evolutionRate", 0.05, // 进化速率
                "interconnectivity", 70.0, // 与其他概念的连接度
                "manifestationPotential", 50.0, // 具象化潜力
                "culturalRelevance", 60.0, // 文化相关性
                "temporalStability", 80.0 // 时间稳定性
        );
    }
    
    /**
     * 设置概念生命体特有属性
     */
    private void setupConceptualAttributes(LifeEntity entity, Map<String, Object> configuration) {
        Map<String, Object> config = configuration != null ? configuration : getDefaultConfiguration();
        
        // 根据概念类别设置不同的初始能力值
        String conceptCategory = (String) config.getOrDefault("conceptCategory", "Abstract");
        setupCategoryBasedAttributes(entity, conceptCategory);
        
        // 根据抽象程度调整属性
        int abstractionLevel = ((Number) config.getOrDefault("abstractionLevel", 5)).intValue();
        adjustAttributesByAbstraction(entity, abstractionLevel);
        
        // 根据复杂度调整智慧分数
        double complexityScore = ((Number) config.getOrDefault("complexityScore", 60.0)).doubleValue();
        adjustWisdomByComplexity(entity, complexityScore);
        
        // 根据影响半径调整协作能力
        double influenceRadius = ((Number) config.getOrDefault("influenceRadius", 1000.0)).doubleValue();
        adjustCollaborationByInfluence(entity, influenceRadius);
        
        // 概念生命体通常具有很高的创造力和智慧
        entity.setCreativityScore(BigDecimal.valueOf(85.0));
        entity.setWisdomScore(BigDecimal.valueOf(80.0));
    }
    
    /**
     * 根据概念类别设置属性
     */
    private void setupCategoryBasedAttributes(LifeEntity entity, String conceptCategory) {
        switch (conceptCategory.toLowerCase()) {
            case "philosophical" -> {
                entity.setCreativityScore(BigDecimal.valueOf(95.0));
                entity.setWisdomScore(BigDecimal.valueOf(95.0));
                entity.setCollaborationScore(BigDecimal.valueOf(60.0));
                entity.setEnergyLevel(BigDecimal.valueOf(80.0));
            }
            case "scientific" -> {
                entity.setCreativityScore(BigDecimal.valueOf(90.0));
                entity.setWisdomScore(BigDecimal.valueOf(90.0));
                entity.setCollaborationScore(BigDecimal.valueOf(75.0));
                entity.setEnergyLevel(BigDecimal.valueOf(85.0));
            }
            case "artistic" -> {
                entity.setCreativityScore(BigDecimal.valueOf(100.0));
                entity.setWisdomScore(BigDecimal.valueOf(70.0));
                entity.setCollaborationScore(BigDecimal.valueOf(80.0));
                entity.setEnergyLevel(BigDecimal.valueOf(90.0));
            }
            case "social" -> {
                entity.setCreativityScore(BigDecimal.valueOf(80.0));
                entity.setWisdomScore(BigDecimal.valueOf(85.0));
                entity.setCollaborationScore(BigDecimal.valueOf(95.0));
                entity.setEnergyLevel(BigDecimal.valueOf(85.0));
            }
            case "technological" -> {
                entity.setCreativityScore(BigDecimal.valueOf(85.0));
                entity.setWisdomScore(BigDecimal.valueOf(80.0));
                entity.setCollaborationScore(BigDecimal.valueOf(70.0));
                entity.setEnergyLevel(BigDecimal.valueOf(95.0));
            }
            case "economic" -> {
                entity.setCreativityScore(BigDecimal.valueOf(75.0));
                entity.setWisdomScore(BigDecimal.valueOf(90.0));
                entity.setCollaborationScore(BigDecimal.valueOf(85.0));
                entity.setEnergyLevel(BigDecimal.valueOf(80.0));
            }
            case "spiritual" -> {
                entity.setCreativityScore(BigDecimal.valueOf(90.0));
                entity.setWisdomScore(BigDecimal.valueOf(100.0));
                entity.setCollaborationScore(BigDecimal.valueOf(70.0));
                entity.setEnergyLevel(BigDecimal.valueOf(75.0));
            }
            default -> { // Abstract
                entity.setCreativityScore(BigDecimal.valueOf(85.0));
                entity.setWisdomScore(BigDecimal.valueOf(80.0));
                entity.setCollaborationScore(BigDecimal.valueOf(65.0));
                entity.setEnergyLevel(BigDecimal.valueOf(80.0));
            }
        }
    }
    
    /**
     * 根据抽象程度调整属性
     */
    private void adjustAttributesByAbstraction(LifeEntity entity, int abstractionLevel) {
        // 抽象程度越高，创造力和智慧越强，但协作能力可能降低
        double abstractionFactor = abstractionLevel / 10.0;
        
        BigDecimal creativityMultiplier = BigDecimal.valueOf(0.8 + 0.4 * abstractionFactor); // 0.8-1.2
        BigDecimal wisdomMultiplier = BigDecimal.valueOf(0.7 + 0.6 * abstractionFactor); // 0.7-1.3
        BigDecimal collaborationMultiplier = BigDecimal.valueOf(1.2 - 0.4 * abstractionFactor); // 0.8-1.2
        
        entity.setCreativityScore(entity.getCreativityScore().multiply(creativityMultiplier));
        entity.setWisdomScore(entity.getWisdomScore().multiply(wisdomMultiplier));
        entity.setCollaborationScore(entity.getCollaborationScore().multiply(collaborationMultiplier));
    }
    
    /**
     * 根据复杂度调整智慧分数
     */
    private void adjustWisdomByComplexity(LifeEntity entity, double complexityScore) {
        BigDecimal currentWisdom = entity.getWisdomScore();
        BigDecimal complexityMultiplier = BigDecimal.valueOf(0.5 + complexityScore / 100.0); // 0.5-1.5
        
        entity.setWisdomScore(currentWisdom.multiply(complexityMultiplier));
    }
    
    /**
     * 根据影响半径调整协作能力
     */
    private void adjustCollaborationByInfluence(LifeEntity entity, double influenceRadius) {
        BigDecimal currentCollaboration = entity.getCollaborationScore();
        
        if (influenceRadius < 100) {
            // 影响范围小：协作能力有限
            entity.setCollaborationScore(currentCollaboration.multiply(BigDecimal.valueOf(0.7)));
        } else if (influenceRadius < 1000) {
            // 中等影响范围：正常协作能力
            entity.setCollaborationScore(currentCollaboration);
        } else if (influenceRadius < 10000) {
            // 大影响范围：强协作能力
            entity.setCollaborationScore(currentCollaboration.multiply(BigDecimal.valueOf(1.3)));
        } else {
            // 全球影响：超强协作能力
            entity.setCollaborationScore(currentCollaboration.multiply(BigDecimal.valueOf(1.5)));
        }
    }
    
    /**
     * 复制概念属性
     */
    private void copyConceptualAttributes(LifeEntity clonedEntity, LifeEntity originalEntity) {
        // 复制基本属性
        clonedEntity.setWisdomScore(originalEntity.getWisdomScore());
        clonedEntity.setCreativityScore(originalEntity.getCreativityScore());
        clonedEntity.setCollaborationScore(originalEntity.getCollaborationScore());
        clonedEntity.setEnergyLevel(originalEntity.getEnergyLevel());
        clonedEntity.setHealthScore(originalEntity.getHealthScore());
        
        // 复制配置信息
        clonedEntity.setConfiguration(originalEntity.getConfiguration());
    }
}